<?php
namespace Activity\Controllers;

use Activity\Libraries\Helper;
use Activity\Models\Deposit;
use Activity\Models\MemAmountLog;
use Activity\Models\Member;
use Activity\Models\RedPacket;
use Activity\Models\RedPacketLog;

class RedpacketController extends BaseController
{
    public function initialize()
    {
    }

    /**
     * 用户领取红包
     *
     */
    public function joinAction()
    {
        $data = [
            'token' => $this->request->getHeader('token')
        ];
        if ($data['token'] == '') {
            return $this->json([
                'code' => 501,
                'msg'  => '用户信息丢失'
            ]);
        }
        $mem  = new Member();
        $user = $mem->getUidByToken($data['token']);
        if ($user == false) {
            return $this->json([
                'code' => 501,
                'msg'  => '用户信息丢失'
            ]);
        }
        $data['uid'] = $user['userid'];
        $log         = new RedPacketLog();
        $isGet       = $log->isGetByToday($data['uid']);
        // 判断今日是否领取过
        if ($isGet === true) {
            return $this->json([
                'code' => 501,
                'msg'  => '您今天已经参与过，请明天再来参加！'
            ]);
        }

        // 判断红包累计是否超过提现金额
        $sum = $log->getSumRedPacketByUid($data['uid']);
        if ($sum['money'] == null) {
            $sum['money'] = 0;
        }

        $redPacket = new RedPacket();
        $ret       = $redPacket->getRedPacketLimit();
        if ($ret === false) {
            return $this->json([
                'code' => 501,
                'msg'  => '暂无活动!'
            ]);
        }
        if ($sum['money'] > $ret['limit_money']) {
            return $this->json([
                'code' => 501,
                'msg'  => '您领取的红包已经超过额度,不能再参与了!'
            ]);
        } else {
            //计算红包金额
            $money = round($ret['limit_money'] / $ret['valid_date'], 2);
            $money = round(Helper::randFloat(0, $money), 2);
            $log->add(['uid' => $data['uid'], 'money' => $money]);
            return $this->json([
                'code'  => 200,
                'msg'   => '恭喜您成功得到了红包',
                'money' => $money
            ]);
        }
    }

    /**
     * 日志获取
     */
    public function logAction()
    {
        $data = [
            'token' => $this->request->getHeader('token'),
            'page'  => $this->request->getPost('page'),
            'size'  => 10
        ];
        if ($data['token'] == '') {
            return $this->json([
                'code' => 501,
                'msg'  => '用户信息丢失'
            ]);
        }
        $mem  = new Member();
        $user = $mem->getUidByToken($data['token']);
        if ($user == false) {
            return $this->json([
                'code' => 501,
                'msg'  => '用户信息丢失'
            ]);
        }
        $data['uid'] = $user['userid'];
        $uid         = $data['uid'];
        $model       = new RedPacket();
        $ret         = $model->getDeposit($uid);
        $logModel    = new RedPacketLog();
        $offset      = $offset = intval(($data['page'] - 1) * $data['size']);
        $log         = $logModel->getLog($uid, $data['size'], $offset);
        $totalMoney  = $logModel->sumMoney($data['uid']);
        return $this->json([
            'code' => 200,
            'msm'  => '获取成功',
            'data' => [
                'log'         => $log !== false ? $log : [],
                'money'       => $ret !== false ? $ret['money'] : 0,
                'total'       => $log !== false ? count($log) : 0,
                'total_money' => $totalMoney !== false ? $totalMoney['money'] : 0
            ]
        ]);
    }

    /**
     * 提现申请
     */

    public function recordAction()
    {
        $data = [
            'token' => $this->request->getHeader('token'),
            'page'  => $this->request->getPost('page'),
            'size'  => 10
        ];
        if ($data['token'] == '') {
            return $this->json([
                'code' => 501,
                'msg'  => '用户信息丢失'
            ]);
        }
        $mem  = new Member();
        $user = $mem->getUidByToken($data['token']);
        if ($user == false) {
            return $this->json([
                'code' => 501,
                'msg'  => '用户信息丢失'
            ]);
        }
        $data['uid'] = $user['userid'];

        $offset = $offset = intval(($data['page'] - 1) * $data['size']);
        $model  = new Deposit();
        $ret    = $model->record($data['uid'], $data['size'], $offset);
        return $this->json([
            'code' => 200,
            'msg'  => '获取成功',
            'data' => $ret !== false ? $ret : []
        ]);
    }

}